<?php
error_reporting(E_ALL);


include_once('controller/includes/constants.php');
include_once('controller/includes/db_access.php');

// we first include the upload class, as we will need it here to deal with the uploaded file
include_once('controller/class.upload/class.upload.php');

// retrieve eventual CLI parameters
$cli = (isset($argc) && $argc > 1);
if ($cli) {
    if (isset($argv[1]))
        $_GET['file'] = $argv[1];
    if (isset($argv[2]))
        $_GET['dir'] = $argv[2];
    if (isset($argv[3]))
        $_GET['pics'] = $argv[3];
}

// set variables
$dir_dest = (isset($_GET['dir']) ? $_GET['dir'] : 'test');

$dir_pics = (isset($_GET['picsis']) ? $_GET['pics'] : $dir_dest);






if ((isset($_POST['action']) ? $_POST['action'] : (isset($_GET['action']) ? $_GET['action'] : '')) == 'multiple') {

    $naam = stripslashes($_POST['naam']);
    $descr = stripslashes($_POST['descr']);
    $categorie_id = $_POST['categorie_id'];
    $huurprijs = stripslashes($_POST['prijs']);
    $merk = stripslashes($_POST['merk']);
    $type = stripslashes($_POST['type']);
    $url = stripslashes($_POST['url']);
    $url_name = stripslashes($_POST['url_name']);
    $aantal = $_POST['aantal'];
    $aktief = $_POST['aktief'];

// Insert a new user into the `user` table  
    $q = new Query;
    $q
            ->insert_into('`product`', array(
                '`categorie_id`' => $categorie_id,
                '`naam`' => $naam,
                '`huurprijs`' => $huurprijs,
                '`descr`' => $descr,
                '`aantal`' => $aantal,
                '`merk`' => $merk,
                '`type`' => $type,
                '`url`' => $url,
                '`url_name`' => $url_name,
                '`aktief`' => $aktief,
                    )
    );

    $result = $q->run();
    $insert_id = $q->get_insert_id();
    if ($result && $insert_id > 0) {

        // ---------- MULTIPLE UPLOADS FOTO1 FOTO2 FOTO3----------
        // as it is multiple uploads, we will parse the $_FILES array to reorganize it into $files
        $files = array();
        foreach ($_FILES['my_field'] as $k => $l) {
            if ($l!='') {
            foreach ($l as $i => $v) {
                if (!array_key_exists($i, $files))
                    $files[$i] = array();
                $files[$i][$k] = $v;
            }
            }
        }

        // now we can loop through $files, and feed each element to the class
        $order = 1;
         if (count($files > 0)) {
        foreach ($files as $file) {

            // we instanciate the class for each element of $file
            $handle = new Upload($file, 'nl_NL');

            // then we check if the file has been uploaded properly
            // in its *temporary* location in the server (often, it is /tmp)
            if ($handle->uploaded) {

                // now, we start the upload 'process'. That is, to copy the uploaded file
                // from its temporary location to the wanted location
                // It could be something like $handle->Process('/home/www/my_uploads/');
                $handle->Process($dir_dest);

                // we check if everything went OK
                if ($handle->processed) {
                     
                    
                    $imgrow='img'.$order;
                        //die($imgrow);
                    $q_Img = new Query;
                    $q_Img->update('`product`')
                            ->set(
                                    array(
                                        'product.'.$imgrow => $handle->file_dst_name
                                    )
                            )
                            ->where_equal_to(
                                    array(
                                        '`product`.`product_id`' => $insert_id
                                    )
                            )
                            ->run();
                    $order++;
                    // everything was fine !
                } else {
                    // one error occured

                    //$Error = '  Error: ' . $handle->error . '';
                }
            } else {
                // if we're here, the upload file failed for some reasons
                // i.e. the server didn't receive the file

               //$Error = '  Error: ' . $handle->error . '';
            }
        }
         }
        
       
                // ---------- MULTIPLE UPLOADS FILE MANUAL EN TECHNICAL ----------
        // as it is multiple uploads, we will parse the $_FILES array to reorganize it into $files
        
        $files_doc = array();
        foreach ($_FILES['my_doc'] as $key => $l) {
            if ($l!='') {
            foreach ($l as $i => $v) {
                if (!array_key_exists($i, $files_doc))
                    $files_doc[$i] = array();
                $files_doc[$i][$key] = $v;
            }
            }
        }

        // now we can loop through $files, and feed each element to the class
        $order2 = 1;
        if (count($files_doc>0)) {
        foreach ($files_doc as $file_doc) {

            // we instanciate the class for each element of $file
            $handle = new Upload($file_doc, 'nl_NL');

            // then we check if the file has been uploaded properly
            // in its *temporary* location in the server (often, it is /tmp)
            if ($handle->uploaded) {

                // now, we start the upload 'process'. That is, to copy the uploaded file
                // from its temporary location to the wanted location
                // It could be something like $handle->Process('/home/www/my_uploads/');
                $handle->Process($dir_dest);

                // we check if everything went OK
                if ($handle->processed) {
                     
                    
                    if($order2 == 1){
                        //die($imgrow);
                    $q_doc = new Query;
                    $q_doc->update('`product`')
                            ->set(
                                    array(
                                        'product.manual' => $handle->file_dst_name
                                    )
                            )
                            ->where_equal_to(
                                    array(
                                        '`product`.`product_id`' => $insert_id
                                    )
                            )
                            ->run();
                    
                            }else  if($order2 == 2){
                        //die($imgrow);
                    $q_doc = new Query;
                    $q_doc->update('`product`')
                            ->set(
                                    array(
                                        'product.technical' => $handle->file_dst_name
                                    )
                            )
                            ->where_equal_to(
                                    array(
                                        '`product`.`product_id`' => $insert_id
                                    )
                            )
                            ->run();
                    
                            }
                    $order2++;
                    // everything was fine !
                } else {
                    // one error occured

                    //$Error = '  Error: ' . $handle->error . '';
                }
            } else {
                // if we're here, the upload file failed for some reasons
                // i.e. the server didn't receive the file

               //$Error =  $handle->error . '';
            }
        }
        }
        
    } else {
        echo 'Sorry, could not add product photo.';
    }
}


?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="styles/screen.css" type="text/css" charset="utf-8" media="screen" />

<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>

    </head>
    <body>
        <table width="900" border="0" cellspacing="5" cellpadding="5">
            <thead>
                <tr>
                    <th colspan="3"><?php include_once('controller/includes/header.php'); ?></th>

                </tr>
            </thead>
            <tbody> 
            <form name="form1" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="cmxform" id="signupForm" >
                <input type="hidden" name="action" value="multiple" />
                <tr>
                    <td width="10%" valign="top"> </td>
                    <td width="50%" valign="top"><font color="red"><?php if (isset($Error)){echo '  Error: ' .$Error;} ?></font> </td>
                    <td width="40%" valign="top"></td>
                </tr>
                <tr>
                    <td>Product Categorie</td>
                    <td>
                        <select name="categorie_id" class="inp" onblur="this.style.backgroundColor='#fff';" id="schilder_id" required="required">
                            <option value="">- selecteer een categorie -</option>
<?php
$queryCAT = "SELECT * FROM categorieen order by categorie asc";
$resultCAT = mysql_query($queryCAT);
$numCAT = mysql_numrows($resultCAT);
for ($z = 0; $z < $numCAT; $z++) {
    ?>

                                <option value="<?php echo mysql_result($resultCAT, $z, "categorie_id"); ?>"><?php echo mysql_result($resultCAT, $z, "categorie"); ?></option>
                            <?php } ?>
                        </select></td>
                    <td></td>
                </tr>

                <tr>
                    <td>Naam: </td>
                    <td><input type="text" name="naam" value="" required="required" minlength="2" /></td>
                    <td></td>
                </tr>
                <tr>
                    <td>merk: </td>
                    <td><input type="text" name="merk" value="" size="20" required="required" /></td>
                    <td></td>
                </tr>
                <tr>
                    <td>type: </td>
                    <td><input type="text" name="type" value="" size="20" required="required" /></td>
                    <td> </td>
                </tr>
                <tr>
                    <td>Beschrijving</td>
                    <td><textarea name="descr" rows="4" cols="20">
                        </textarea></td>
                    <td></td>
                </tr>
                <tr>
                    <td  valign="top">
                        <p>Foto 1<br /><br /></p>

                        <p>Foto 2<br /><br /></p>

                        <p>Foto 3</p>

                    </td>
                    <td  valign="top">
                        <p><input type = "file" size="32" name="my_field[]" value="" required="required" /></p>
                        <p><input type = "file" size="32" name="my_field[]" value="" required="required" /></p>
                        <p><input type = "file" size="32" name="my_field[]" value="" required="required" /></p>
                        <p class="button"></p>
                    </td>
                    <td></td>
                </tr>

                <tr>
                    <td>Aantal</td>
                    <td><input type="text" name="aantal" value="" required="required" /></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Huur Prijs (incl BTW)</td>
                    <td><input type="text" name="prijs" value="" required="required" /></td>
                    <td></td>
                </tr>
                <tr>
                    <td>
                        <p>Manual<br /><br /></p>
                        <p>Technical</p>
                    </td>
                    <td>
                        <p><input type="file" name="my_doc[]" value="" width="20" /> (pdf of doc)</p>
                        <p><input type="file" name="my_doc[]" value="" width="20" /> (pdf of doc)</p>
                    </td>
                    <td></td>
                </tr>
             
                <tr>
                    <td >Link</td>
                    <td><input type="text" name="url" value="" /></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Link naam</td>
                    <td><input type="text" name="url_name" value="" /></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Beschikbaar</td>
                    <td><input type="radio" name="aktief" value="Y" />ja <input type="radio" name="aktief" value="N" /> nee</td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td><input type="submit" value="save" name="save" /></td>
                </tr>   
            </form>
        </tbody>
    </table>

</body>
</html>
